package com.eqdd.yiqidian.utils;

import java.lang.reflect.Field;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 17-12-8  上午11:03.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class SplitUtil {
    public static String combin(List<String> list, String split) {
        if (ListUtil.isEmpty(list)) {
            return null;
        }
        StringBuilder stringBuilder = null;
        for (String s : list) {
            if (stringBuilder == null) {
                stringBuilder = new StringBuilder(s);
            } else {
                stringBuilder.append(split).append(s);
            }
        }
        return stringBuilder == null ? null : stringBuilder.toString();
    }

    public static String combin(List list, String split, String param) {
        if (ListUtil.isEmpty(list)) {
            return null;
        }
        StringBuilder stringBuilder = null;
        try {
            stringBuilder = null;
            for (Object s : list) {
                if (stringBuilder == null) {
                    stringBuilder = new StringBuilder(getField(s, param));
                } else {
                    stringBuilder.append(split).append(getField(s, param));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return stringBuilder == null ? null : stringBuilder.toString();
    }

    private static String getField(Object o, String field) throws Exception {
        Field declaredField = o.getClass().getDeclaredField(field);
        declaredField.setAccessible(true);
        return declaredField.get(o) + "";
    }
}
